const app = getApp();
Page({
	CustomBar: app.globalData.CustomBar,

	data: {
		history: [],
		hot: [],
		searchKey: '',
		groups: [{
			text: '确定',
			value: 1,
			currentIndex: 0
		}],

		// 2-1 test
		ColorList: app.globalData.ColorList,
		color: 'red',
		TabCur: 0,
		scrollLeft: 0,
		StatusBar: app.globalData.StatusBar,
		CustomBar: app.globalData.CustomBar,
		Custom: app.globalData.Custom,
		hasUserInfo: false,
		canIUse: wx.canIUse('button.open-type.getUserInfo'),
		TabCur: 1,
		scrollLeft: 0,
		isLogin: app.globalData.isLogin,
		userInfo: app.globalData.userInfo,
		showSettings: false,
		gridCol: 3,
		FList: ['可回收垃圾', '厨余垃圾', '有害垃圾', '其它垃圾'],
		onLoad: function () {
			this.data.FList.forEach((item, index) => {
				console.log(item);
			})
		}

	},

// 跳转
	navWenda: function () {
		wx.navigateTo({
			url: '/pages/weekly/Wenda/Wenda',
		})
	},
	navsuggestion: function () {
		wx.navigateTo({
			url: '/pages/weekly/Suggestion/Suggestion',
		})
  },
  navAnswer:function(){
    wx.navigateTo({
      url: '/pages/answer/answer-home/answer-home',
    })
  },

	navTongzhi: function () {
		wx.navigateTo({
			url: '/pages/weekly/Tongzhi/Tongzhi',
		})
  },

  changeToTalk:function(){
    wx.navigateTo({
      url: '/pages/talking/talking',
		})
  },

	// 登录
	loginSuccess(userInfo) {
		app.$db.set('userInfo', userInfo)
		this.setData({
			userInfo: userInfo,
			nickName: userInfo.nickName
		})
		app.globalData.userInfo = userInfo
		app.globalData.isLogin = true
		wx.hideLoading({})
		app.$util.reLaunch("/pages/index/index")
		this.setData({
			isLogin: true
		});
	},

	getUserInfo: function (e) {
		console.log(e)
		app.globalData.userInfo = e.detail.userInfo
		this.setData({
			userInfo: e.detail.userInfo,
			hasUserInfo: true
		})
	},

	showModal(e) {
		this.setData({
			modalName: e.currentTarget.dataset.target
		})
	},

	hideModal(e) {
		this.setData({
			modalName: null
		})
	},

	tabSelect(e) {
		console.log(e);
		this.setData({
			TabCur: e.currentTarget.dataset.id,
			scrollLeft: (e.currentTarget.dataset.id - 1) * 60
		})
	},

	tabSelect(e) {
		this.setData({
			TabCur: e.currentTarget.dataset.id,
			scrollLeft: (e.currentTarget.dataset.id - 1) * 60
		})
	},

	onShow() {
		if (app.globalData.isLogin) {
			this.setData({
				isLogin: true
			})
			this.setData({
				userInfo: app.globalData.userInfo,
			})
		} else {
			wx.showModal({
				title: '提示',
				content: '还未登录，请先登录！',
				showCancel: false,
				success: (res) => {
					if (res.confirm) {
						app.$util.switchTabTo("/pages/user/user")
					}
				}
			})
			this.setData({
				isLogin: false
			})
		}

		if (this.data.isLogin) {
			wx.cloud.callFunction({
				name: "db",
				data: {
					$url: "getUserInfo"
				}
			}).then(res => {
				if (res.result.code) {
					app.$db.set('userInfo', res.result.userInfo)
					this.setData({
						userInfo: res.result.userInfo,
						nickName: res.result.userInfo.nickName
					})
					app.globalData.userInfo = res.result.userInfo
				}
			})
		}
	},

	onLoad() {
		let that = this;
		setTimeout(function () {
			that.setData({
				loading: true
			})
		}, 500);
		this.setData({
			isLogin: app.globalData.isLogin,
			userInfo: app.globalData.userInfo,
			nickName: this.data.userInfo && this.data.userInfo.nickName,
			history: app.$db.get('history') || []
		})
		this.getHot()
	},

	showModal(e) {
		this.setData({
			modalName: e.currentTarget.dataset.target
		})
	},

	hideModal(e) {
		this.setData({
			modalName: null
		})
	},

	SetColor(e) {
		this.setData({
			color: e.currentTarget.dataset.color,
			modalName: null
		})
	},

	SetActive(e) {
		this.setData({
			active: e.detail.value
		})
	},

	wxGetUserInfo() {
		var that = this;
		wx[wx.getUserProfile ? 'getUserProfile' : 'getUserInfo']({
			desc: '用于完善会员资料',
			success: (e) => {
				if (e.userInfo) {
					let userInfo = e.userInfo
					wx.showLoading({
						title: '登录中',
					})
					wx.cloud.callFunction({
						name: "db",
						data: {
							$url: "login",
							userInfo: {
								nickName: userInfo.nickName,
								gender: userInfo.gender,
								avatarUrl: userInfo.avatarUrl
							}
						}
					}).then(res => {
						if (res.result.code) {
							wx.showToast({
								title: '登录成功',
								icon: 'success',
								duration: 1500
							})
							this.loginSuccess(res.result.userInfo)
						}
					})
				} else {
					wx.showModal({
						title: '警告',
						content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
						showCancel: false,
						confirmText: '返回授权',
						success: function (res) {
							if (res.confirm) {}
						}
					});
				}
			},
			fail: (err) => {
				wx.showModal({
					title: '警告',
					content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
					showCancel: false,
					confirmText: '返回授权',
					success: function (res) {
						if (res.confirm) {}
					}
				});
			}
		})
	},

	loginSuccess(userInfo) {
		app.$db.set('userInfo', userInfo)
		this.setData({
			userInfo: userInfo,
			nickName: userInfo.nickName
		})
	},
	// 2-1 test
	Tapbutton: function () {
		let val = '测试1' 
		let devIntegral = app.deductionIntegral.get('garbageTextSearch')
		wx.showLoading({
			title: '正在加载数据中.....',
		})
		wx.cloud.callFunction({ //垃圾文字搜索
			name: 'api',
			data: {
				$url: "garbageTextSearch",
				text: val,
				devIntegral: devIntegral
			}
		}).then(res => { //然后执行：
			wx.hideLoading();
			let result = res.result

			if (result.ret == 200) {
				this.setData({
					dialogShow: true
				})
				let info = result.data.list[0]
				app.globalData.userInfo.integral -= devIntegral
				app.$db.set('userInfo', app.globalData.userInfo)
				this.setData({
					garbage: info
				})
			} else {
				wx.showToast({
					title: result.msg,
					icon: 'none',
					duration: 1500
				})
			}

			if (this.data.history.indexOf(val) == -1) {
				if (this.data.history.length > 100) {
					this.data.history = this.data.history.shift()
				}
				this.setData({
					history: this.data.history.concat(val)
				})
				this.addHot(val)
				app.$db.set('history', this.data.history)
			}
		})

	},

	openDelAction() {
		this.setData({
			showActionsheet: true
		})
	},

	closeDelAction: function () {
		this.setData({
			showActionsheet: false
		})
	},

	delHnader(e) {
		if (e.detail.value) {
			this.setData({
				history: []
			})
			app.$db.set('history', this.data.history)
		}
		this.closeDelAction()
	},

	closeDialog() {
		this.setData({
			dialogShow: false
		})
	},

	getHot() {
		wx.cloud.callFunction({
			name: 'db',
			data: {
				$url: "getHot"
			}
		}).then(res => {
			let data = res.result.data
			let hot = data && data.map(item => item.hot_text)
			this.setData({
				hot: hot || []
			})
		})
	},

	addHot(hot_text) {
		wx.cloud.callFunction({
			name: 'db',
			data: {
				$url: "addHot",
				hot_text: hot_text
			}
		})
	},

	// 点击标签，自动写入
	tagClick(e) {
		this.setData({
			searchKey: e.currentTarget.dataset.tag
		})
	},

	delHnader(e) {
		if (e.detail.value) {
			this.setData({
				history: []
			})
			app.$db.set('history', this.data.history)
		}
		this.closeDelAction()
	},
})